namespace YXShop.ExcelLite
{
    using System;
    using System.Reflection;

    public sealed class ExcelColumnCollection : ExcelRowColumnCollectionBase
    {
        internal ExcelColumnCollection(ExcelWorksheet parent) : base(parent)
        {
        }

        internal ExcelColumnCollection(ExcelWorksheet parent, ExcelColumnCollection sourceColumns) : base(parent)
        {
            base.MaxOutlineLevel = sourceColumns.MaxOutlineLevel;
            foreach (ExcelColumn column in sourceColumns)
            {
                base.Items.Add(new ExcelColumn(this, column));
            }
        }

        private void AdjustArraySize(int index)
        {
            if (index > (base.Items.Count - 1))
            {
                ExceptionIfColumnOutOfRange(index);
                int num = index - (base.Items.Count - 1);
                for (int i = 0; i < num; i++)
                {
                    base.Items.Add(new ExcelColumn(this, base.Items.Count));
                }
            }
        }

        public static string ColumnIndexToName(int columnIndex)
        {
            char ch = (char) (0x41 + (columnIndex % 0x1a));
            columnIndex /= 0x1a;
            if (columnIndex == 0)
            {
                return ch.ToString();
            }
            char ch2 = (char) (0x40 + columnIndex);
            return (ch2.ToString() + ch.ToString());
        }

        public static int ColumnNameToIndex(string name)
        {
            int letterIndex = -1;
            if (name.Length == 1)
            {
                letterIndex = GetLetterIndex(name[0]);
            }
            else if (name.Length == 2)
            {
                letterIndex = ((GetLetterIndex(name[0]) + 1) * 0x1a) + GetLetterIndex(name[1]);
            }
            if ((letterIndex < 0) || (letterIndex > 0xff))
            {
                throw new ArgumentOutOfRangeException("name", name, "Column name must be one-letter or two-letter name from A to IV.");
            }
            return letterIndex;
        }

        internal static void ExceptionIfColumnOutOfRange(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", index, "Index can't be negative.");
            }
            int num = 0xff;
            if (index > num)
            {
                throw new ArgumentOutOfRangeException("index", index, "Index can't be lager than maximum column index (" + num + ").");
            }
        }

        private static int GetLetterIndex(char letter)
        {
            int num = char.ToUpper(letter) - 'A';
            if ((num < 0) || (num > 0x19))
            {
                throw new ArgumentOutOfRangeException("letter", letter, "Column name must be made from valid letters of English alphabet.");
            }
            return num;
        }

        public ExcelColumn this[int index]
        {
            get
            {
                this.AdjustArraySize(index);
                return (ExcelColumn) base.Items[index];
            }
        }

        public ExcelColumn this[string name]
        {
            get
            {
                return this[ColumnNameToIndex(name)];
            }
        }
    }
}

